/**
 * Created by chenzheyu on 2017/4/17.
 */
$(function () {


    $.ajax({
        url: ctx + '/rentappsetting/selectRentAppSetting',
        data: {},
        type: 'GET',
        success: function (data) {
            var Data = data;
            console.log(Data);
            Data.isDifferentShop = 1;
            if (data.isRemote == 0) {
                $('.isRemote .No').prop('checked', 'checked');
            } else {
                $('.isRemote .Yes').prop('checked', 'checked');
            }
            if (data.isDifferentShop == 0) {
                $('.isDifferentShop .No').prop('checked', 'checked');
            } else {
                $('.isDifferentShop .Yes').prop('checked', 'checked');
            }

            console.log($().Timestamp.TimestampDate(data.gatherStartTime));
            console.log($().Timestamp.TimestampDate(data.gatherStartTime).split(' ')[1].split('--')[0]);

            $('.counterFee input').val(data.counterFee);
            $('.basicPremium input').val(data.basicPremium);
            $('.nightService input').val(data.nightService);
            $('.gatherStartTime input').val($().Timestamp.TimestampDate(data.gatherStartTime).split(' ')[1].split('--')[0]).data('time', data.gatherStartTime);
            $('.gatherEndTime input').val($().Timestamp.TimestampDate(data.gatherEndTime).split(' ')[1].split('--')[0]).data('time', data.gatherEndTime);
            $('.serviceStartTime input').val($().Timestamp.TimestampDate(data.serviceStartTime).split(' ')[1].split('--')[0]).data('time', data.serviceStartTime);
            $('.serviceEndTime input').val($().Timestamp.TimestampDate(data.serviceEndTime).split(' ')[1].split('--')[0]).data('time', data.serviceEndTime);
            $('.childSafetyCost input').val(data.childSafetyCost);
            $('.matchDriver input').val(data.matchDriver);
            $('.viewDay input').val(data.viewDay);
            $('.offsiteReturnFee input').val(data.offsiteReturnFee);
            $('.difShopReturnFee input').val(data.difShopReturnFee);
            $('.dispatchCarFee input').val(data.dispatchCarFee);

            // $('.FeeCharging').DateTimeSelect({
            //     onChange: function () {
            //         Data.gatherStartTime = $().Timestamp.DateTimestamp("1970-01-01 " + $('.FeeCharging').val());
            //         var time = $().Timestamp.DateTimestamp("1970-01-01 " + $('.FinishCharge').val());
            //         console.log(time)
            //         if (time - Data.gatherStartTime >= 10800) {
            //             Data.gatherStartTime = $().Timestamp.DateTimestamp("1970-01-01 " + $('.FeeCharging').val());
            //         } else if (time - Data.gatherStartTime == 0) {
            //             Data.gatherStartTime = $().Timestamp.DateTimestamp("1970-01-01 " + $('.FeeCharging').val());
            //             $('body').Overlays({data: {text: '您当前选择的是24小时！', state: 'Warn'}});
            //         } else {
            //             $('body').Overlays({data: {text: '结束时间必须大于开始时间3小时，请重新选择！', state: 'No'}});
            //             Data.gatherStartTime = $().Timestamp.DateTimestamp("1970-01-01 " + $('.FeeCharging').val());
            //             $('.FinishCharge').val('')
            //         }
            //     }, MinutesInterval: 30
            // });
            // $('.FinishCharge').DateTimeSelect({
            //     onChange: function () {
            //         Data.gatherStartTime = $().Timestamp.DateTimestamp("1970-01-01 " + $('.FeeCharging').val());
            //         var time = $().Timestamp.DateTimestamp("1970-01-01 " + $('.FinishCharge').val());
            //         console.log(time);
            //         console.log(time);
            //         if (time - Data.gatherStartTime >= 10800) {
            //             Data.gatherEndTime = time;
            //         } else if (time - Data.gatherStartTime == 0) {
            //             $('body').Overlays({data: {text: '您当前选择的是24小时！', state: 'Warn'}});
            //             Data.gatherEndTime = time;
            //         } else {
            //             $('body').Overlays({data: {text: '结束时间必须大于开始时间3小时，请重新选择！', state: 'No'}});
            //             $('.FinishCharge').val('')
            //         }
            //     }, MinutesInterval: 30
            // });
            $('.StartService').DateTimeSelect({
                onChange: function () {
                    Data.serviceStartTime = $().Timestamp.DateTimestamp("1970-01-01 " + $('.StartService').val());
                    var time = $().Timestamp.DateTimestamp("1970-01-01 " + $('.EndService').val());
                    console.log(time)
                    if (time - Data.serviceStartTime >= 10800) {
                        Data.serviceStartTime = $().Timestamp.DateTimestamp("1970-01-01 " + $('.StartService').val());
                    } else if (time - Data.serviceStartTime == 0) {
                        $('body').Overlays({data: {text: '您当前选择的是24小时！', state: 'Warn'}});
                        Data.serviceStartTime = $().Timestamp.DateTimestamp("1970-01-01 " + $('.StartService').val());
                    } else {
                        $('body').Overlays({data: {text: '结束时间必须大于开始时间3小时，请重新选择！', state: 'No'}});
                        $('.EndService').val('')
                    }
                }, MinutesInterval: 30
            });
            $('.EndService').DateTimeSelect({
                onChange: function () {
                    Data.serviceStartTime = $().Timestamp.DateTimestamp("1970-01-01 " + $('.StartService').val());
                    var time = $().Timestamp.DateTimestamp("1970-01-01 " + $('.EndService').val());
                    console.log(time)
                    if (time - Data.serviceStartTime >= 10800) {
                        Data.serviceEndTime = time;
                    } else if (time - Data.serviceStartTime == 0) {
                        $('body').Overlays({data: {text: '您当前选择的是24小时！', state: 'Warn'}});
                        Data.serviceEndTime = time;
                    } else {
                        $('body').Overlays({data: {text: '结束时间必须大于开始时间3小时，请重新选择！', state: 'No'}});
                        $('.EndService').val('')
                    }

                }, MinutesInterval: 30
            });

            $('.counterFee input').on('change', function () {
                Data.counterFee = $(this).val();
            });
            $('.basicPremium input').on('change', function () {
                Data.basicPremium = $(this).val();
            });
            $('.nightService input').on('change', function () {
                Data.nightService = $(this).val();
            });
            $('.childSafetyCost input').on('change', function () {
                Data.childSafetyCost = $(this).val();
            });
            $('.matchDriver input').on('change', function () {
                Data.matchDriver = $(this).val();
            });
            $('.viewDay input').on('change', function () {
                Data.viewDay = $(this).val();
            });
            $('.offsiteReturnFee input').on('change', function () {
                Data.offsiteReturnFee = $(this).val();
            });
            $('.difShopReturnFee input').on('change', function () {
                Data.difShopReturnFee = $(this).val();
            });
            $('.dispatchCarFee input').on('change', function () {
                Data.dispatchCarFee = $(this).val();
            });





            for (var i = 0; i < $('.isRemote input[type="radio"]').length; i++) {
                (function () {
                    var index = i;
                    $('.isRemote input[type="radio"]').eq(index).on('click', function () {
                        console.log($(this))
                        for (var j = 0; j < $('.isRemote input[type="radio"]').length; j++) {
                            if ($('.isRemote input[type="radio"]').eq(j).prop('checked')) {
                                if ($('.isRemote input[type="radio"]').eq(j).siblings().eq(0).text() == '是') {
                                    Data.isRemote = 1
                                } else {
                                    Data.isRemote = 0;
                                }
                            }
                        }
                    })
                })()
            }




            $('.ActionOk').on('click', function () {
                $.ajax({
                    url: ctx + '/rentappsetting/updaterentappsetting',
                    data: Data,
                    type: 'POST',
                    success: function (data) {
                        if (data.message == '修改成功') {
                            $('body').Overlays({data: {text: '保存成功！', state: 'Ok'}});
                        }
                    }
                })
            })
        }
    })


    for(var i=0;i<$('input[type="number"]').length;i++){
        (function () {
            var index=i;
            $('input[type="number"]').eq(index).on('change',function () {
                if($(this).val()<0){
                    $(this).val(0)
                }
            })
        })()
    }
});